﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace WorldBuilder
{
    public class LevelData
    {
        enum TextureColor
        {
            Red,
            Green,
            Blue, 
            Yellow,
            Orange,
            Black,
            White,

        };
        int m_currentColor;

        enum LevelType
        {
            OBLIQUE_2D,
            ISO_2D,
            Standard_3D,            
        };

        int m_currentLevelType;
        string m_description;

        string m_storedLocation;

        int m_MapSizeX;
        int m_MapSizeY;

        // Only important for 2D 
        int m_tileSizeX;
        int m_tileSizeY;

        // Only important for 2D
        int[,] m_map;
        string[,] m_tiles;

        VertexDeclaration myVertrexDeclaration;
        VertexPositionColorTexture[] m_vertices;
        int m_verticesCount;



        // The data types have to be setup so we can allocate when the LevelData is created.
        // 3D Constructor 
        public LevelData(int levelType, int mapSizeX, int mapSizeY, int verticesCount, GraphicsDevice device)
        {
        
            try
            {
                // Throw an error if we are not using the right constructor
                if (levelType != 2)
                {
                    throw new ArgumentException();

                }
                m_currentLevelType = levelType;
                m_MapSizeX = mapSizeX;
                m_MapSizeY = mapSizeY;
                m_verticesCount = verticesCount;
                m_currentColor = 0;

                m_storedLocation = "";

                SetUpVertices();
            }
            catch
            {

            }
        }

        //2D Constructor
        public LevelData(int levelType, int mapSizeX,int mapSizeY,int tileSizeX, int tileSizeY)
        {
            m_currentLevelType = levelType;
            m_MapSizeX = mapSizeX;
            m_MapSizeY = mapSizeY;
            m_tileSizeX = tileSizeX;
            m_tileSizeY = tileSizeY;
            m_storedLocation = "";

            // If we are loading a 2D world we will want to have a map for storing this data.  
            SetUpMap();
           
                
            
        }
        private void SetUpMap()
        {
            m_map = new int[m_MapSizeX, m_MapSizeY];
            //Set them all up as Tile Zero

            for (int x = 0; x < m_MapSizeX; x++)
            {
                for (int y = 0; y < m_MapSizeY; y++)
                {
                    m_map[x, y] = 0;
                }
            }

            m_tiles = new string[0, 0];
        }

        private void SetUpVertices()
        {
            Color RenderColor = Color.White;


            m_vertices = new VertexPositionColorTexture[m_verticesCount];

            for (int x = 0; x < m_verticesCount; x++)
            {
               RenderColor = GetColor(RenderColor);
                // Will have to have an algorithm for Placement
                m_vertices[x].Position = new Vector3(0, 0, 0); 
                m_vertices[x].Color = RenderColor;
                //m_vertices[x].TextureCoordinate //Deal with this later. 

            }
        }

        // Ugly but a way to get Color
        private Color GetColor(Color color)
        {
             
            if (m_currentColor == 0)
            {
                color = Color.Red;
            }
            else if (m_currentColor == 1)
            {
                color = Color.Green;
            }
            else if (m_currentColor == 2)
            {
                color = Color.Blue;

            }
            else if (m_currentColor == 3)
            {
                color = Color.Yellow;
            }
            else if (m_currentColor == 4)
            {
                color = Color.Orange;

            }
            else if (m_currentColor == 5)
            {
                color = Color.Black;
            }
            else if (m_currentColor == 6)
            {
                color = Color.White;
            }

            if (m_currentColor < 6)
            { m_currentColor++; }
            else
            { m_currentColor = 0; }

            return color;
        }


        public int SetDescription(string Description)
        {
            m_description = Description;

            return 0;
        }
        public string GetDescription()
        {
            return m_description;
        }

        public int GetTileSizeX()
        {
            return m_tileSizeX;
        }


        public int GetTileSizeY()
        {
            return m_tileSizeY;
        }


        public int GetMapSizeX()
        {
            return m_MapSizeX;
        }

        public int GetMapSizeY()
        {
            return m_MapSizeY;
        }

        public string GetLevelProjection()
        {
            string Projection="";
            switch (m_currentLevelType)
            {
                case 0:
                    Projection = "2D Oblique";
                    break;
                case 1:
                    Projection = "2D Isometric";
                    break;
            }


            return Projection;
        }


        public int[,] GetMap()
        {
            return m_map;
        }

        public string GetMap_string()
        {
            string map="";

            for (int x = 0; x < m_MapSizeX; x++)
            {
                for (int y = 0; y < m_MapSizeY; y++)
                {
                    map = map + m_map[x, y].ToString();

                }
            } 

            return map;
        }

        public string GetVertices_string()
        {
            string vertices;

            vertices = "";
            for (int x = 0; x < m_verticesCount; x++)
            {
                vertices += m_vertices[x].Position.ToString();
                vertices += m_vertices[x].Color.ToString();
            }
            return vertices;
        }

        public void SetMapSizeX(string MapSizeX)
        {
            m_MapSizeX = Int32.Parse(MapSizeX);

        }
        public void SetMapSizeY(string MapSizeY)
        {
            m_MapSizeY = Int32.Parse(MapSizeY);            
        }

        public void SetTileSizeX(string TileSizeX)
        {
            m_tileSizeX = Int32.Parse(TileSizeX);
        }
        public void SetTileSizeY(string TileSizeY)
        {
            m_tileSizeY = Int32.Parse(TileSizeY);
        }
        public void SetMap(string map)
        {
            m_map = new int[m_MapSizeX, m_MapSizeY];

            try
            {
                for (int x = 0; x < m_MapSizeX; x++)
                {
                    for (int y = 0; y < m_MapSizeY; y++)
                    {
                        m_map[x, y] = Int32.Parse(map[x * y].ToString());
                    }
                }
            }
            catch
            {
            }

        }

        public void SetVertices(string vertData)
        {


        }
  
        
    }
}
